package test.lyj;

import java.util.HashMap;
import java.util.Map;

import json.NormalObject;

import org.json.JSONException;


import test.lyj.model.NetThread;
import test.lyj.tool.Jsonformat2Bean;
import test.lyj.tool.Tool;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class CurveIndexActivity extends Activity {
	
	//延迟多少次
	private int loadingSecond=10;
	private String username;
	private String psw;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		String result=null;
		
		/*从上一个activity传值到这个activity 这里没用到
		 * if (getIntent() != null) {
			Bundle bundle = getIntent().getExtras();
			if (bundle != null) {
				result= bundle.getString("myPara");
			
			}
		}*/
		super.onCreate(savedInstanceState);

		setContentView(R.layout.logon_window);
		
		
		EditText usernameview=(EditText) findViewById(R.id.username_logon);
		 username=usernameview.getText().toString();
		EditText pswview=(EditText) findViewById(R.id.psw_logon);
		 psw=pswview.getText().toString();
		findViewById(R.id.btn_logon).setOnClickListener(netClicker);
		
		
		//netthread.run();
	}
	OnClickListener netClicker = new OnClickListener() {

		
		public void onClick(View v) {
			Map<String,String> paramap=new HashMap<String,String>();
			paramap.put("op", "logon");
			paramap.put("username", username);
			paramap.put("psw", psw);
			Log.d("LogonActivity", "提交的参数:username:"+username+"|psw:"+psw);
			//线程的启动方法在NetThread 的构造函数中
			new NetThread(CurveIndexActivity.this,messageListener,paramap,null);

		}
	};
	
	Handler messageListener = new Handler() {
		public void handleMessage(Message msg) {
			Bundle b = msg.getData();
			String netresult = b.getString(Tool.TIMER_TASK_RESULT);
			switch (msg.what) {
			
			case Tool.TIMER_TASK_TIMEOUT:
				Tool.showMsgDialog(CurveIndexActivity.this, "线程超时"+netresult);
				break;
			case Tool.TIMER_TASK_COMPELETE:
				
				Tool.showMsgDialog(CurveIndexActivity.this,"网络结果:"+netresult);
				try {
					NormalObject object=(NormalObject) Jsonformat2Bean.formatJsonStr2Bean(netresult);
					Intent intent = new Intent();
					intent.setClass(CurveIndexActivity.this, ImgBaseActivity.class);
					startActivity(intent);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					Tool.showMsgDialog(CurveIndexActivity.this,"错误:"+e.getMessage());
				}
				break;
			
			}
			super.handleMessage(msg);
		}
	};
	
	/*@Override
	protected void onPause() {
		Log.d(this.getClass().getName(), "onPause!");
		// TODO Auto-generated method stub
		
		Bundle bundle =new Bundle();
		Intent intent=new Intent();
		bundle.putString("lyj_getcode", "这里显示的是返回的网络结果!");
		intent.putExtras(bundle);
		setResult(100,intent);
		super.onPause();
	}*/
	
	/**
	 * 复写,界面返回时候调用的方法
	 */
	public boolean onKeyDown(final int keyCode, final KeyEvent event) {
		// if(KeyEvent.KEYCODE_HOME==keyCode||KeyEvent.KEYCODE_BACK==keyCode){
		if (KeyEvent.KEYCODE_BACK == keyCode) {
			Bundle bundle =new Bundle();
			Intent intent=new Intent();
			bundle.putString("lyj_getcode", "这里显示的是返回的网络结果!");
			intent.putExtras(bundle);
			setResult(100,intent);
			//这句话必须调用,不然传递参数后,界面仍然存在,不跳转
			CurveIndexActivity.this.finish();
		}
		return false;
	}

	
	
	

}
